﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class CollectRewardItemCommand extends Command {

        public var event:InfoEvent;
        public var worldModel:WorldModel;
        public var pendingModel:PendingModel;
        private var _rewardItemId:int;
        private var _pendingVO:PendingItemVO;

        override public function execute():void{
            var _local2:RewardItemVO;
            var _local1:IRoom = (this.event.info as IRoom);
            var _local3:int;
            while (_local3 < _local1.rewardItems.length) {
                _local2 = _local1.rewardItems[_local3];
                if (this.event.id == _local2.id){
                    this._rewardItemId = _local3;
                    this.createPendingVO(_local2);
                    this.addPendingItem();
                    this.collectItem(_local2);
                    break;
                };
                _local3++;
            };
        }
        private function createPendingVO(_arg1:RewardItemVO):void{
            var _local2:String = _arg1.itemType;
            if ((((_local2 == GameConstants.REWARD_COFFEE)) || ((_local2 == GameConstants.REWARD_COFFEE_PACK)))){
                _local2 = GameConstants.COFFEE;
            };
            if ((((_local2 == GameConstants.REWARD_XP)) || ((_local2 == GameConstants.REWARD_XP_PACK)))){
                _local2 = GameConstants.XP;
            };
            this._pendingVO = new PendingItemVO(_local2, int(_arg1.amount));
        }
        private function addPendingItem():void{
            this.pendingModel.addPendingItem(this._pendingVO);
        }
        private function removePendingItem():void{
            this.pendingModel.removePendingItem(this._pendingVO);
        }
        private function collectItem(_arg1:RewardItemVO):void{
            l(("c collectItem: " + this.collectItem));
            var _local2:Object = {taskType:GameConstants.COLLECT_ITEM, itemType:_arg1.itemType, onResult:this.onResult};
            var _local3:Array = [new RequestVO("collect_item", {id:_arg1.id})];
            var _local4:TaskRequestEvent = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local3, _local2);
            dispatch(_local4);
            dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_HARVEST_GEM));
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:IRoom;
            var _local4:RewardItemVO;
            var _local5:int;
            if (_arg1.success){
                _local2 = _arg1.data.collect_item;
                _local3 = (this.event.info as IRoom);
                _local5 = 0;
                while (_local5 < _local3.rewardItems.length) {
                    _local4 = _local3.rewardItems[_local5];
                    if (this.event.id == _local4.id){
                        _local3.rewardItems.splice(_local5, 1);
                        break;
                    };
                    _local5++;
                };
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, _local2));
            };
            this.removePendingItem();
        }

    }
}//package net.wooga.pets.controllers 
